package stripesbook.action.user;

import java.util.Collection;
import java.util.List;
import org.stripesbook.rayures.view.ViewObject;
import org.stripesbook.rayures.view.ViewTag;
import org.stripesbook.rayures.view.stripes.SErrors;
import org.stripesbook.rayures.view.stripes.SFile;
import org.stripesbook.rayures.view.stripes.SForm;
import org.stripesbook.rayures.view.stripes.SHidden;
import org.stripesbook.rayures.view.stripes.SImage;
import org.stripesbook.rayures.view.stripes.SLabel;
import org.stripesbook.rayures.view.stripes.SOptionsCollection;
import org.stripesbook.rayures.view.stripes.SSelect;
import org.stripesbook.rayures.view.stripes.SSubmit;
import org.stripesbook.rayures.view.stripes.SText;
import org.stripesbook.rayures.view.stripes.STextArea;
import stripesbook.action.BaseActionBean;
import stripesbook.model.Attachment;
import stripesbook.model.Contact;

public class MessageComposeForm extends SForm {
  private static final String ARROW = "/images/arrow.png";

  @Override
  public void onInit() {
    super.onInit();
    beanclass(MessageComposeActionBean.class);
  }

  public ViewObject<?> getErrors() {
    return tag(ViewObject.class)
      .add(tag(SErrors.class).field("message.to"))
      .add(tag(SErrors.class).field("message.subject"));
  }

  public ViewObject<?> getHeaderFor(String field, String imageName) {
    return tag(ViewObject.class)
      .add(tag(ViewTag.class).tagName("th").add(tag(SLabel.class).forId(field)).add(":"))
      .add(tag(ViewTag.class).tagName("td").add(tag(SText.class).name(field).size(60)))
      .add(tag(ViewTag.class).tagName("td").add(tag(SImage.class).name(imageName).src(ARROW)));
  }

  public SSelect getSelectContacts() {
    Collection<Contact> contacts = getActionBean().getUser().getContacts();
    return tag(SSelect.class).name("contacts").multiple(true).size(7)
      .add(tag(SOptionsCollection.class).collection(contacts).value("id").sort("firstName"));
  }

  public SLabel getSubjectLabel() {
    return tag(SLabel.class).forId("message.subject");
  }

  public SText getSubjectField() {
    return tag(SText.class).name("message.subject").size(60);
  }

  public STextArea getTextArea() {
    return tag(STextArea.class).name("message.text").cols(87).rows(12);
  }

  public SErrors getErrorsForAttachments() {
    return tag(SErrors.class).field("attachments");
  }

  public ViewObject<?> getAttachmentFields() {
    ViewObject<?> viewObject = tag(ViewObject.class);

    for (int i = 0; i < 4; i++) {
      viewObject.add(tag(ViewTag.class).tagName("div")
        .add(tag(SFile.class).name("attachments[" + i + "]")));
    }
    return viewObject;
  }

  public SSubmit getUploadButton() {
    return tag(SSubmit.class).name("upload");
  }

  public SHidden getDeleteIndex() {
    return tag(SHidden.class).id("deleteIndex").name("deleteIndex");
  }

  public ViewObject<?> getAttachments() {
    ViewObject<?> viewObject = tag(ViewObject.class);

    List<Attachment> attachments = ((MessageComposeActionBean) getActionBean())
      .getMessage().getAttachments();

    int t = attachments == null ? 0 : attachments.size();

    for (int i = 0; i < t; i++) {
      viewObject.add(tag(SImage.class).name("deleteAttachment").src("/images/delete.gif")
        .onclick("getElementById('deleteIndex').value=" + i)
        .style("border: none; vertical-align: bottom"));

      Attachment attachment = attachments.get(i);

      viewObject.add(attachment.getFileName()).add(" (")
        .add(attachment.getSize()).add(" bytes)");

      viewObject.add("<br/>");
    }
    return viewObject;
  }

  public ViewObject<?> getSubmitButtons() {
    return tag(ViewObject.class)
      .add(tag(SSubmit.class).name("send"))
      .add(tag(SSubmit.class).name("cancel"));
  }

  private BaseActionBean getActionBean() {
    return ((BaseActionBean) viewContext.getActionBean());
  }
}
